package com.zishi.venuesbooking.ac;

import android.os.Bundle;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageButton;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zishi.venuesbooking.BaseActivity;
import com.zishi.venuesbooking.R;
import com.zishi.venuesbooking.adapter.MessageAdapter;
import com.zishi.venuesbooking.entity.Message;

import org.json.JSONException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class chatActivity extends BaseActivity {

    @Override
    protected int initLayout() {
        return R.layout.activity_chat;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() throws JSONException, IOException {

    }

    private RecyclerView rvMessages;
    private EditText etInput;
    private ImageButton btnSend;
    private MessageAdapter adapter;
    private List<Message> messages = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);

        initViews();
        setupRecyclerView();
        loadMessages();

        btnSend.setOnClickListener(v -> sendMessage());
        etInput.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEND) {
                sendMessage();
                return true;
            }
            return false;
        });
    }

    private void initViews() {
        rvMessages = findViewById(R.id.rv_messages);
        etInput = findViewById(R.id.et_input);
        btnSend = findViewById(R.id.btn_send);
    }

    private void setupRecyclerView() {
        adapter = new MessageAdapter(messages, getCurrentUserId());
        rvMessages.setLayoutManager(new LinearLayoutManager(this));
        rvMessages.setAdapter(adapter);

        // 新消息到达时自动滚动到底部
        adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
            @Override
            public void onItemRangeInserted(int positionStart, int itemCount) {
                rvMessages.smoothScrollToPosition(adapter.getItemCount());
            }
        });
    }

    private void loadMessages() {
        // 从数据库或网络加载消息
        // messages.addAll(...);
        // adapter.notifyDataSetChanged();
    }

    private void sendMessage() {
        String content = etInput.getText().toString().trim();
        if (content.isEmpty()) return;

        Message message = new Message();
        message.setContent(content);
        message.setTimestamp(System.currentTimeMillis());
        message.setType(Message.TYPE_SENT);
        message.setSenderName("我");

        messages.add(message);
        adapter.notifyItemInserted(messages.size() - 1);
        etInput.setText("");

        // 发送消息到服务器
        // sendMessageToServer(message);
    }

    private String getCurrentUserId() {
        // 返回当前用户ID
        return "user123";
    }
}